package com.app.teacherActivity;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.app.R;
import com.app.bean.NoticeBean;
import com.app.util.DBHelper1;
import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;

public class TeacherDetail extends AppCompatActivity {

    private TextView name, content, time;
    private Button delete, edit;
    private NoticeBean bean;
    private ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_teacher_detail);
        initView();
        //获取上一个界面传递过来的数据
        bean = (NoticeBean) getIntent().getSerializableExtra("bean");
        //给TextView设置显示的内容
        name.setText(bean.getName());
        content.setText(bean.getContent());
        time.setText(bean.getTime());
//        imageView.setImageResource(bean.getImage());
        Glide.with(this).load(bean.getPic()).into(imageView);
        delete.setOnClickListener(v -> {
            DBHelper1 dbHelper = new DBHelper1(getApplicationContext());
            SQLiteDatabase db = dbHelper.getWritableDatabase();
            int idToDelete = bean.getId();
            String selection = "id=?";
            String[] selectionArgs = {String.valueOf(idToDelete)};
            int deletedRows = db.delete("notice", selection, selectionArgs);
            if (deletedRows > 0) {
                Toast.makeText(this, "删除成功", Toast.LENGTH_SHORT).show();
                finish();
            } else {
                Toast.makeText(this, "删除失败", Toast.LENGTH_SHORT).show();
            }
            db.close();
        });
        edit.setOnClickListener(v -> {
            //跳转界面，并把数据传递过去，传递数据的名字是bean
            Intent intent = new Intent(this, Add.class);
            intent.putExtra("bean",bean);
            startActivityForResult(intent,0);
        });
    }

    private void initView() {
        Toolbar toolbar = findViewById(R.id.toolbar);
        toolbar.getChildAt(0).setOnClickListener(v -> finish());
        ((TextView) toolbar.getChildAt(1)).setText("通知详细");

        name = findViewById(R.id.name);
        content = findViewById(R.id.content);
        time = findViewById(R.id.time);
        delete = findViewById(R.id.delete);
        edit = findViewById(R.id.edit);
        imageView = findViewById(R.id.detail_image);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode==RESULT_OK) {
            bean = (NoticeBean) data.getSerializableExtra("bean");
            name.setText(bean.getName());
            content.setText(bean.getContent());
            time.setText(bean.getTime());
            Glide.with(this).load(bean.getPic()).into(imageView);
        }
    }
}